package word_queue;

import com.rabbitmq.client.*;
import utils.RabbitMqUtils;

import java.io.IOException;

public class Customer2 {
    public static void main(String[] args) throws IOException {
        //获取连接对象
        Connection connection = RabbitMqUtils.getConnection();
        //获取通道
        final Channel channel = connection.createChannel();
        //每次从AMQP中得到一个消息
        channel.basicQos(1);
        //通过通道绑定一个队列
        channel.queueDeclare("work", false, false, false, null);
        //消费消息
        channel.basicConsume("work", false, new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                Thread thread=new Thread();
                try {
                    thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("消费者2："+new String(body));
                //手动确认，参数1：手动确认消息标识，参数2：false 每次确认一个
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        });
    }

}
